<extend name="Public:common"/>
<block name="css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css"  media="all">
    <style>
        .newMess th{text-align:center;}
        th,td{padding: 10px;}
    </style>
</block>
<block name="content">
    <div class="page-header"><h1><i class="fa fa-home"></i> 首页>商家管理>交通供应商>添加交通方式</h1></div>
    <div class="container-fluid">
        <form action="" onsubmit="return check()" method="post">
            <input type="hidden" value="{:I('supplier_id')}" name="supplier_id">
            <table class="table table-striped table-bordered table-hover table-condensed text-center">
                <tbody>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>交通方式</b></td>
                    <td class="text-left">
                        <select name="traffic_id" id="traffic_id" onchange="getSeatByTrafficId()" required>
                            <option value="">==请选择交通方式==</option>
                            <volist name="trafficList" id="vo">
                                <option value="{$vo.traffic_id}">{$vo.traffic_name}</option>
                            </volist>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>座位</b></td>
                    <td class="text-left" id="seatList">
                        <!--<div class="checkbox">
                            <label for="room_{$vo.room_id}">
                                <input class="chk" type="checkbox" id="room_{$vo.room_id}" value="{$vo.room_id}" data-name="{$vo.room_name}">{$vo.room_name}
                            </label>
                        </div>-->
                    </td>
                </tr>

                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>出发城市</b></td>
                    <td class="text-left">

                        国家
                        <select name="start_country" id="ar_country_id"  onchange="getProvince(this,'ar')" required>
                            <option value="">==选择国家==</option>
                            <volist name="country" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        省
                        <select name="start_province" id="ar_province_id"  onchange="getCity(this,'ar')" required>
                            <option value="">==选择省==</option>
                            <volist name="province" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        市
                        <select name="start_city" id="ar_city_id"  onchange="getArea(this,'ar')" required>
                            <option value="">==选择市==</option>
                            <volist name="city" id="vo">
                                <option value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        区
                        <select name="start_area"   id="ar_area_id"  required>
                            <option value="">==选择区==</option>
                            <volist name="area" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>目的城市</b></td>
                    <td class="text-left">
                        国家
                        <select name="goal_country" id="goal_country_id"  onchange="getProvince(this,'goal')" required>
                            <option value="">==选择国家==</option>
                            <volist name="country" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        省
                        <select name="goal_province" id="goal_province_id"  onchange="getCity(this,'goal')" required>
                            <option value="">==选择省==</option>
                            <volist name="province" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        市
                        <select name="goal_city" id="goal_city_id"  onchange="getArea(this,'goal')" required>
                            <option value="">==选择市==</option>
                            <volist name="city" id="vo">
                                <option value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                        区
                        <select name="goal_area"   id="goal_area_id" required >
                            <option value="">==选择区==</option>
                            <volist name="area" id="vo">
                                <option  value="{$vo.areaId}">{$vo.areaName}</option>
                            </volist>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>线路名称</b></td>
                    <td class="text-left">
                        <input type="text" name="routes_name" value="" required>
                    </td>
                </tr>
                <tr>
                    <td style="width:25%;"><b> <span class="required">*</span>币种</b></td>
                    <td class="text-left">
                        <input type="text" name="money_type"  value="{$currency[$unit_supplier_info['currency_id']]}" readonly>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="width:25%;">
                        <p>
                            <b> <span class="required">*</span>价格信息</b>
                        </p>

                        <table class="table table-striped table-bordered table-hover table-condensed text-center" id="addTr">
                            <tbody>
                            <tr>
                                <td style="width:15%">座位类型</td>
                                <td style="width:15%">儿童成本价</td>
                                <td style="width:15%">成人成本价</td>
                                <td style="width:15%">座椅数量</td>
                            </tr>

                            </tbody>
                        </table>
                    </td>
                </tr>

                <tr>
                    <td colspan="2">
                        <a class="btn btn-info" href="{:U('trafficProductById',array('id'=>I('supplier_id')))}">返回</a>
                        <!-- <a href="{:U('trafficShops')}" class="btn btn-info">返回</a>-->
                        <input type="submit" class="btn btn-primary" value="保存" >
                    </td>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</block>
<block name="js">
    <script>

        /**
         * 判断有没有选择
         */
        function check() {
            var  ischecked=false;
            $('.chk').each(function(){
                if($(this).prop('checked')){
                    ischecked=true;
                    return false;
                }
            });
            layer.msg('请选择座位!');
            return ischecked;
        }



        //选择交通方式,获取相应座位表
        function getSeatByTrafficId() {
            //清空之前其它信息
            $('#addTr tbody tr:gt(0)').remove();
            $('#seatList').empty();
            var trafficId=$('#traffic_id').val();
            $.ajax({
                type: "POST",
                url: "{:U('ajaxGetSeatByTrafficId')}",
                data: {
                    trafficId:trafficId
                },
                dataType: "json",
                success: function(data){
                    var html='';
                    for(var i in data){
                        html+='  <div class="checkbox">';
                        html+=' <label for="room_'+data[i].seat_id+'">';
                        html+=' <input class="chk"  type="checkbox" id="room_'+data[i].seat_id+'" value="'+data[i].seat_id+'" data-name="'+data[i].seat_name+'">'+data[i].seat_name;
                        html+='</label>';
                        html+='</div>';
                    }
                    $('#seatList').append(html);
                }
            });

        }


        //监听checkbox选中状态
        $('body').on('change','.chk',function () {
            addTr(this);

        });
        //根据汇率自动转换价格
        var foreign=['child_foreign_cost','adult_foreign_cost'];
        var actual=['child_actual_cost','adult_actual_cost'];
        var _foreign,_actual;
        $('body').on('change','.related',function () {
            var name=$(this).attr('name');
            var val=$(this).val();
            var multiply_divide=1;//乘除
            if(name.indexOf('foreign')==-1){
                _foreign=actual;
                _actual=foreign;
                multiply_divide=0;
            }else{
                _foreign=foreign;
                _actual=actual;
                multiply_divide=1;
            }
            for(var i in _foreign){
                //汇率
                var exchangeName=name.replace(_foreign[i],'exchange_rate');
                var exchange_rate=  $('input[name=\''+exchangeName+'\']').val();
                if(!isNaN(exchange_rate)){
                    if(name.indexOf(_foreign[i])!=-1){
                        var changeName=name.replace(_foreign[i],_actual[i]);
                        var result=(val*exchange_rate).toFixed(2);
                        if(multiply_divide){
                            result=(val/exchange_rate).toFixed(2);
                        }
                        $('input[name=\''+changeName+'\']').val(result);
                    }
                }
            }
        });

        //改变汇率更换值
        $('body').on('change','.exchange_rate',function () {
            var reateName=$(this).attr('name');
            var val=$(this).val();
            for(var i in foreign){
                var name=reateName.replace('exchange_rate',foreign[i]);
                var tempVal=$('input[name=\''+name+'\']').val();
                if(tempVal){
                    var actualName=reateName.replace('exchange_rate',actual[i]);
                    var result=(tempVal/val).toFixed(2);
                    $('input[name=\''+actualName+'\']').val(result);
                }else{
                    var name=reateName.replace('exchange_rate',actual[i]);
                    var tempVal=$('input[name=\''+name+'\']').val();
                    if(tempVal){
                        var result=(tempVal*val).toFixed(2);
                        var foreignName=reateName.replace('exchange_rate',foreign[i]);
                        $('input[name=\''+foreignName+'\']').val(result);
                    }
                }
            }
        });


        //添加一行
        function addTr(_this) {
            var name=$(_this).attr('data-name');
            var room_ids=$(_this).val();
            if($(_this).is(':checked')){
                var html=' <tr id="tr_'+room_ids+'">';
                html+='<td class="td_'+room_ids+'">'+name+' <input type="hidden" name="room['+room_ids+'][room_id]" value="'+room_ids+'"></td>';
                html+='<td class="td_'+room_ids+'"><input class="form-input related"  onkeyup="num(this)"   type="text" name="room['+room_ids+'][child_actual_cost]" required ></td>';
                html+='<td class="td_'+room_ids+'"><input class="form-input related"  onkeyup="num(this)"   type="text" name="room['+room_ids+'][adult_actual_cost]" required></td>';
                html+='<td class="td_'+room_ids+'"><input class="form-input"   onkeyup="num(this)"  type="text" name="room['+room_ids+'][room_count]" min="1" required></td>';
                html+='</tr>';
                $("#addTr tr:last-child").after(html);
            }else{
                if($('body').find('#tr_'+room_ids).length>0){
                    $('#tr_'+room_ids).remove();
                }
            }

        }

        //限制只能输入2位小数
        function num(obj)
        {
            //先把非数字的都替换掉，除了数字和.
            obj.value = obj.value.replace(/[^\d.]/g,"");
            //必须保证第一个为数字而不是.
            obj.value = obj.value.replace(/^\./g,"");
            //保证只有出现一个.而没有多个.
            obj.value = obj.value.replace(/\.{2,}/g,".");
            //保证.只出现一次，而不能出现两次以上
            obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj.value=obj.value.substring(0,5);
        }

    </script>
</block>